﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VswService_V7.Utility;
using System.Data;
using VswService_V7.DataBase.Tables.L2INTERFACE;

namespace VswService_V7.DataBase.Tables.L3INTERFACE
{
    public class L3INTF_BATCH
    {
        #region field
        public static readonly string tableName = "L3INTF_BATCH";
        public static readonly string fd_eventNo = "EVENT_NO";
        public static readonly string fd_taskNo = "TASK_NO";
        public static readonly string fd_stripNo = "STRIP_NO";
        public static readonly string fd_seq = "SEQ";
        public static readonly string fd_batchNoE = "BATCH_NO_E";
        public static readonly string fd_batchNoO = "BATCH_NO_O";
        public static readonly string fd_updateTime = "UPDATE_TIME";
        #endregion
        /// <summary>
        /// response l3 interface batch,success=2
        /// </summary>
        /// <param name="eventNo"></param>
        public static void ResponseIntfBatch(string eventNo)
        {
            try
            {
                string sqlINTFBATCH = string.Format("select * from {0} where {1}={2}",
                    tableName, fd_eventNo, eventNo);
                DataTable batchTable = OracleHelper.Exct_QueryTable(sqlINTFBATCH);
                List<string> sqlList = new List<string>();
                if (batchTable != null && batchTable.Rows.Count > 0)
                {
                    for (int i = 0; i < batchTable.Rows.Count; i++)
                    {
                        if (batchTable.Rows[i][fd_batchNoO] != null && batchTable.Rows[i][fd_batchNoO].ToString() != "")
                        {
                            string batchNoE = "";
                            if (batchTable.Rows[i][fd_batchNoE] != null && batchTable.Rows[i][fd_batchNoE].ToString() != "")
                            {
                                batchNoE = batchTable.Rows[i][fd_batchNoE].ToString();
                            }

                            string stripNo = "";
                            if (batchTable.Rows[i][fd_stripNo] != null && batchTable.Rows[i][fd_stripNo].ToString() != "")
                            {
                                stripNo = batchTable.Rows[i][fd_stripNo].ToString();
                            }

                            string batchNoO = batchTable.Rows[i][fd_batchNoO].ToString();
                            string sqlINTFEVENT = string.Format("UPDATE {0} SET {1}='{2}', {3}='{4}',{5}='{6}' where {7}='{8}'",
                                   STRIP_INFO.tableName, STRIP_INFO.fd_batchNoE, batchNoE, STRIP_INFO.fd_batchNoO, batchNoO,STRIP_INFO.fd_status,STRIP_INFO.scaned, STRIP_INFO.fd_stripNo, stripNo
                                  );
                            sqlList.Add(sqlINTFEVENT);
                        }
                    }

                    //close the event
                    string sqlCloseEvent = string.Format("update {0} set {1}='{2}' where {3}={4}",
                    L3INTF_EVENT.tableName, L3INTF_EVENT.fd_eventStatus, 2, L3INTF_EVENT.fd_eventNo, eventNo);
                    sqlList.Add(sqlCloseEvent);

                    for(int i=0;i<sqlList.Count;i++)
                    {
                        LogHelper.WriteLog($"sql do: {sqlList[i]}");
                    }
                    LogHelper.WriteLog($"ResponseIntfBatch Success,update strip count: {sqlList.Count}");

                    OracleHelper.ExecuteSqlTran(sqlList);
                }
                
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("ResponseIntfBatch", ex);
                //close the event
                string sqlCloseEvent = string.Format("update {0} set {1}='{2}' where {3}={4}",
                L3INTF_EVENT.tableName, L3INTF_EVENT.fd_eventStatus, -1, L3INTF_EVENT.fd_eventNo, eventNo);
                throw ex;
            }
        }
    }
}
